<?php

namespace app\admin\model;

use think\Model;


class UserCategoryRmb extends Model
{


    // 表名
    protected $name = 'user_category_rmb';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    public function category()
    {
        return $this->belongsTo('Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    //获取指定用户在指定门店的余额
    public static function GetAmount($user_id, $category_id)
    {
        $amount = self::where(['user_id' => $user_id, 'category_id' => $category_id])->value('amount');
        //如果没有记录，就返回0
        if (!$amount) {
            $amount = 0;
        }
        return $amount;
    }

    //读取指定用户在所有门店余额合计
    public static function GetMyAllCategoryAmount($user_id)
    {
        $amount = self::where(['user_id' => $user_id])->sum('amount');
        //如果没有记录，就返回0
        if (!$amount) {
            $amount = 0;
        }
        return $amount;
    }

    //金额改变
    public static function change(
        $user_id, $category_id,
        $amount,
        $remark
    )
    {
        $user_category_rmb = self::where(['user_id' => $user_id, 'category_id' => $category_id])->find();
        if (!$user_category_rmb) {
            $user_category_rmb = new self();
            $user_category_rmb->user_id = $user_id;
            $user_category_rmb->category_id = $category_id;
            $user_category_rmb->amount = 0;
        }
        $before = $user_category_rmb->amount;
        $after = $user_category_rmb->amount + $amount;

        $user_category_rmb->amount = $user_category_rmb->amount + $amount;
        $user_category_rmb->save();

        //记录日志
        $user_category_rmb_log = new UserCategoryRmbLog();
        $user_category_rmb_log->user_id = $user_id;
        $user_category_rmb_log->category_id = $category_id;
        $user_category_rmb_log->before = $before;
        $user_category_rmb_log->money = $amount;
        $user_category_rmb_log->after = $after;
        $user_category_rmb_log->memo = $remark;
        $user_category_rmb_log->createtime = time();
        $user_category_rmb_log->save();

    }

    // 升级
    public static function upLevel($user_id, $category_id, $rule_id)
    {
        $data = RechargeRule::where('id', $rule_id)->where('category_id', $category_id)->find();
        $user_category_rmb = self::where(['user_id' => $user_id, 'category_id' => $category_id])->find();
        if ($user_category_rmb->level < $data['level'] && $data['level'] > 0) {
            $user_category_rmb->level = $data['level'];
            $user_category_rmb->discount = $data['discount'];
            $user_category_rmb->save();
        }
    }

    public static function getMemberLevel($user_id, $category_id)
    {
        $data = self::where('user_id', $user_id)->where('category_id', $category_id)->find();
        if ($data) {
            return $data['discount'] * 10 / 100;
        }
        return 10 * 10 / 100;
    }
}
